home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / malloc / mallocbug.c < prev    next >
C/C++ Source or Header  |  1993-07-18  |  2KB  |  67 lines

  1. /* Reproduce a GNU malloc bug.  */
  2. #include <stdio.h>
  3. extern char *malloc ();
  4.  
  5. #define size_t unsigned int
  6.  
  7. int
  8. main ()
  9. {
  10.   char *dummy0;
  11.   char *dummy1;
  12.   char *fill_info_table1;
  13.   char *over_top;
  14.   size_t over_top_size = 0x3000;
  15.   char *over_top_dup;
  16.   size_t over_top_dup_size = 0x7000;
  17.   char *x;
  18.   size_t i;
  19.  
  20.   /* Here's what memory is supposed to look like (hex):
  21.         size  contents
  22.         3000  original_info_table, later fill_info_table1
  23.       3fa000  dummy0
  24.       3fa000  dummy1
  25.         6000  info_table_2
  26.     3000  over_top
  27.  
  28.     */
  29.   /* mem: original_info_table */
  30.   dummy0 = malloc (0x3fa000);
  31.   /* mem: original_info_table, dummy0 */
  32.   dummy1 = malloc (0x3fa000);
  33.   /* mem: free, dummy0, dummy1, info_table_2 */
  34.   fill_info_table1 = malloc (0x3000);
  35.   /* mem: fill_info_table1, dummy0, dummy1, info_table_2 */
  36.  
  37.   x = malloc (0x1000);
  38.   free (x);
  39.   /* mem: fill_info_table1, dummy0, dummy1, info_table_2, freexx */
  40.  
  41.   /* This is what loses; info_table_2 and freexx get combined unbeknownst
  42.      to mmalloc, and mmalloc puts over_top in a section of memory which
  43.      is on the free list as part of another block (where info_table_2 had
  44.      been).  */
  45.   over_top = malloc (over_top_size);
  46.   over_top_dup = malloc (over_top_dup_size);
  47.   memset (over_top, 0, over_top_size);
  48.   memset (over_top_dup, 1, over_top_dup_size);
  49.  
  50.   for (i = 0; i < over_top_size; ++i)
  51.     if (over_top[i] != 0)
  52.       {
  53.     printf ("FAIL: malloc expands info table\n");
  54.     return 0;
  55.       }
  56.  
  57.   for (i = 0; i < over_top_dup_size; ++i)
  58.     if (over_top_dup[i] != 1)
  59.       {
  60.     printf ("FAIL: malloc expands info table\n");
  61.     return 0;
  62.       }
  63.  
  64.   printf ("PASS: malloc expands info table\n");
  65.   return 0;
  66. }
  67.